ShopScene1={}
ShopScene1Func={}
ShopScene1Func.__index = ShopScene1Func
setmetatable(ShopScene1, ShopScene1Func)

ShopScene1Func.getNode = function()
  local l = {}
  setmetatable(l, ShopScene1Func)
  l.node = CCBReader:load("ShopScene1.ccbi")
  print("ShopScene1.getNode", l.node)
  --l.node:registerScriptTouchHandler(onTouchShopLayer, false, 0, true)
  --l.node:setTouchEnabled(true)
  ShopScene1.instance = l
  l:init() 
  return l 
end

ShopScene1Func.getScene = function()
  local sc = CCScene:create()
  sc:addChild(ShopScene1.getNode().node)
  print("ShopScene1.getScene", sc)
  return sc
end

ShopScene1Func.init = function(self)
  setObjectExtTable(self.node:getChildByTag(1111), self)
  --self:initTable()
  LoadScene:showLoadingScene()
  NetworkRequest.shops("onGetShopComplete", self)
end
ShopScene1Func.onGetShopComplete = function(self, data)
  print("ShopScene1Func.onGetShopComplete", data)
  LoadScene:removeLoadingScene()
  local res = cjson.decode(data)
  if res.ret==0 then
    self.shopData = res
    self.radioMenu = RadioMenu:create(self.node:getChildByTag(1001), "menuItemChanged", self)
    self.radioMenu:setMenuState(0)
  else
    showMessage(res.ret, ccc3(255,255,0))
  end 
end
ShopScene1Func.menuItemChanged = function(self, oldIndex, newIndex)
  if self.contentLayer~=nil then
    self.node:removeChild(self.contentLayer.node, true)
  end
  if self.curTip~=nil then
    self.node:removeChild(self.curTip, true)
    self.curTip = nil
    --self:enableAllButton(true)
  end
  if newIndex==0 then
    self:initTable(3)
  elseif newIndex==1 then
    self:initTable(4)
  elseif newIndex==2 then
    self:initTable(5)
  end
end
ShopScene1Func.initTable = function(self, id)
  local shops = self.shopData.data.shops[tostring(id)] 
  self.shops = shops
  local c = math.floor((#shops-1)/2) + 1
  local items = CCArray:create()
  --self.buttons = {}
  for i=1, c do
    local item = CCBReader:load("ShopItem1.ccbi")
    local index = (c-i)*2 + 1
    local tmp = Item:createWithId(shops[index].id)
    print("ShopScene1Func.initTable", i, index, shops[index].id, tmp.itemConfig.name, tmp.itemConfig.asset)
    --addSpriteFromBK(item:getChildByTag(2001), 1001, nameFromPath(tmp.itemConfig.asset))
    local b = addButtonFromBK(item:getChildByTag(2001), 1001, nameFromPath(tmp.itemConfig.asset), "", "onClickItem", self)
    b:setTag(tonumber(shops[index].id))

    TypeCast(item:getChildByTag(2001):getChildByTag(1011), "CCLabelTTF"):setString(tmp.itemConfig.name)
    TypeCast(item:getChildByTag(2001):getChildByTag(1012), "CCLabelTTF"):setString(tostring(tmp.itemConfig.coin))
    local b = item:getChildByTag(2001):getChildByTag(1020)
    setObjectExtTable(b, self)
    b:setTag(index) 
    --self.buttons[#self.buttons+1] = b
    if shops[index+1]~=nil then
      tmp = Item:createWithId(shops[index+1].id)
      print("ShopScene1Func.initTable", i, index+1, shops[index].id, tmp.itemConfig.name, tmp.itemConfig.asset)
      --addSpriteFromBK(item:getChildByTag(2002), 1001, nameFromPath(tmp.itemConfig.asset))
      local b = addButtonFromBK(item:getChildByTag(2002), 1001, nameFromPath(tmp.itemConfig.asset), "", "onClickItem", self)
      b:setTag(tonumber(shops[index+1].id))
      TypeCast(item:getChildByTag(2002):getChildByTag(1011), "CCLabelTTF"):setString(tmp.itemConfig.name)
      TypeCast(item:getChildByTag(2002):getChildByTag(1012), "CCLabelTTF"):setString(tostring(tmp.itemConfig.coin))
      local b = item:getChildByTag(2002):getChildByTag(1020)
      setObjectExtTable(b, self)
      b:setTag(index+1)
      --self.buttons[#self.buttons+1] = b
    else
      local l = item:getChildByTag(2002)
      setTouchEnabledAll(l, false)
      l:setVisible(false)
    end
    item:setContentSize(CCSizeMake(800, 200))
    items:addObject(item)
  end
  if self.table~=nil then
    self.node:removeChild(self.table, true)
  end
  local tableview = CCTableView:create(1, CCSizeMake(810, 400), items)
  tableview:setPosition(80, 60)
  self.node:addChild(tableview)
  self.table = tableview
end
ShopScene1Func.onTouchTipLayer = function(event, x, y)
  if event==CCTOUCHBEGAN then
    if ShopScene1.instance.curTip~=nil then
      print("onTouchTipLayer clear tip")
      ShopScene1.instance.node:removeChild(ShopScene1.instance.curTip, true)
      ShopScene1.instance.curTip = nil
    end
  end
  return true 
end
--ShopScene1Func.enableAllButton = function(self, enable)
--  for k,v in pairs(self.buttons) do
--    TypeCast(v, "CCButton"):setEnabled(enable)
--  end
--end
ShopScene1Func.onClickItem = function(self, node, event)
  if self.curTip~=nil then
    self.node:removeChild(self.curTip, true)
    self.curTip = nil
  end
  local item = Item:createWithId(node:getTag()) 
  local tip = item:createTip1()
  self.curTip = tip
  self.node:addChild(self.curTip)
  self.curTip:registerScriptTouchHandler(ShopScene1Func.onTouchTipLayer, false, -1111, true)
  self.curTip:setTouchEnabled(true)
  --self:enableAllButton(false)
end
ShopScene1Func.onBuyButton = function(self, node, event)
  self.currentButton = node
  local index = node:getTag()
  local shops = self.shops 
  NetworkRequest.buyItem(shops[index].id, 1, "onBuyComplete", self) 
  LoadScene:showLoadingScene()
end
ShopScene1Func.onBuyComplete = function(self, data)
  LoadScene:removeLoadingScene()
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    local shops = self.shops 
    local index = self.currentButton:getTag()
    local tmp = Item:createWithId(shops[index].id)
    --local x,y = self.currentButton:getPosition()
    local sp = CCSprite:create(nameFromPath(tmp.itemConfig.asset))
    local pos = self.currentButton:getParent():getChildByTag(1001):convertToWorldSpace(ccp(0,0))
    sp:setPosition(pos)
    self.node:addChild(sp)

    local ma = CCMoveTo:create(0.5, ccp(20, 640))
    local ac = ActionUtil.createSeqAction({ma, CCCallFuncN:create(ShopScene1Func.onActionDone)})
    sp:runAction(ac)
  else
    showMessage(res.msg, ccc3(255, 255, 0)) 
  end
end
ShopScene1Func.onActionDone = function(node)
  node:getParent():removeChild(node, true)
end
ShopScene1Func.onCloseButton = function(self, node, event)
  MainScene.scene:removeChild(node:getParent(), true)
  setTouchEnabledAll(MainScene.node, true)
  MainScene.setState()
  ShopScene1.instance = nil
end

